home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Lose Your Marbles! 1.0 / source / Shell ƒ / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  11.3 KB  |  352 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program in a file named "GNU General Public License".
  19. If not, write to the Free Software Foundation, 675 Mass Ave,
  20. Cambridge, MA 02139, USA.
  21.  
  22. \**********************************************************************/
  23.  
  24. #include "graphics.h"
  25. #include "main.h"
  26. #include "apple events.h"
  27. #include "integrity.h"
  28. #include "about.h"
  29. #include "help.h"
  30. #include "menus.h"
  31. #include "prefs.h"
  32. #include "environment.h"
  33. #include "progress.h"
  34. #include "error.h"
  35. #include "sounds.h"
  36. #include "ls.h"
  37. #include "program globals.h"
  38.  
  39. void main(void)
  40. {
  41.     Boolean            programIntegrityVerified;
  42.     Boolean            programIntegritySet;
  43.     
  44.     /* do integrity check before anything else; see integrity.c for details */
  45.     programIntegrityVerified=DoIntegrityCheck(&programIntegritySet);
  46.     
  47.     /* standard program initialization stuff */
  48.     MaxApplZone();    
  49.     InitGraf(&thePort);
  50.     InitFonts();
  51.     FlushEvents(everyEvent, 0);
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs(0L);
  56.     InitCursor();
  57.     GetDateTime((unsigned long*)&randSeed);
  58.     
  59.     if (!InitTheEnvironment())            /* gestalt checks and variable initialization */
  60.         HandleError(kSystemTooOld, TRUE);        /* less than system 4.1 */
  61.     
  62.     if (!programIntegrityVerified)    /* integrity check failed */
  63.         HandleError(kProgramIntegrityNotVerified, TRUE);
  64.     
  65.     if (programIntegritySet)    /* integrity check freshly installed */
  66.         HandleError(kProgramIntegritySet, FALSE);
  67.     
  68.     if (!InitTheGraphics())        /* initialize offscreen gworlds/bitmaps, etc */
  69.         HandleError(kNoMemoryAndQuitting, TRUE);
  70.     
  71.     if (!InitTheMenus())        /* get menus from .rsrc and draw menu bar */
  72.         HandleError(kProgramIntegrityNotVerified, TRUE);
  73.         
  74.     InitTheSounds();                /* see if sound is available, etc. */
  75.     
  76.     PrefsError(PreferencesInit());    /* get prefs (create if necessary) */
  77.     
  78.     InitTheProgram();                /* program-specific initialization */
  79.     
  80.     EventLoop();                    /* where it all happens (see below) */
  81.     
  82.     ShutDownEnvironment();            /* where it all ends (see below) */
  83.     
  84.     ExitToShell();
  85. }
  86.  
  87. void EventLoop(void)
  88. {
  89.     while (!gDone)    /* gDone set by choosing "Quit" menu item or by "quit" apple event */
  90.         HandleSingleEvent();
  91. }
  92.  
  93. Boolean HandleSingleEvent(void)
  94. {
  95.     EventRecord        theEvent;
  96.     short            i;
  97.     
  98.     SetCursor(&arrow);        /* should set once every time through event loop */
  99.     HiliteMenu(0);            /* normalize menubar */
  100.     
  101.     gFrontWindowIsOurs=FALSE;
  102.     gFrontWindowIndex=0;
  103.     if (FrontWindow()!=0L)    /* if there's a front window, see if it's one of ours */
  104.     {
  105.         for (i=0; ((i<NUM_WINDOWS) && (!gFrontWindowIsOurs)); i++)
  106.         {
  107.             if (FrontWindow()==GetIndWindowGrafPtr(i))    /* found one of ours, so SetPort */
  108.             {
  109.                 SetPort(GetIndWindowGrafPtr(i));
  110.                 gFrontWindowIsOurs=TRUE;
  111.                 gFrontWindowIndex=i;
  112.             }
  113.         }
  114.     }
  115.         
  116.     /* get an event from the queue */
  117.     GetTheEvent(&theEvent, gIsInBackground ? gBackgroundWaitTime : gForegroundWaitTime);
  118.     
  119.     DispatchEvents(theEvent);    /* handle the event we just got */
  120.     
  121.     return (theEvent.what!=nullEvent);
  122. }
  123.  
  124. void GetTheEvent(EventRecord *theEvent, short waitTime)
  125. {
  126.     if (gWaitNextEventAvailable)
  127.         WaitNextEvent(everyEvent, theEvent, waitTime, 0L);
  128.     else
  129.     {
  130.         GetNextEvent(everyEvent, theEvent);
  131.         SystemTask();
  132.     }
  133. }
  134.  
  135. void DispatchEvents(EventRecord theEvent)
  136. {
  137.     short            i;
  138.     Point            thisPoint;
  139.     short            index;
  140.     unsigned long    dummy;
  141.     WindowPtr        theWindow;
  142.     Boolean            thisWindowIsOurs;
  143.     
  144.     thisWindowIsOurs=FALSE;
  145.     /* for update/activate events, see if the window in question is one of ours */
  146.     if ((theEvent.what==activateEvt) || (theEvent.what==updateEvt))
  147.     {
  148.         for (i=0; ((i<NUM_WINDOWS) && (!thisWindowIsOurs)); i++)
  149.             thisWindowIsOurs=((WindowPtr)theEvent.message==GetIndWindowGrafPtr(i));
  150.     }
  151.  
  152.     if (thisWindowIsOurs)    /* for activate/update events, get window index */
  153.         index=(**((WindowDataHandle)GetWRefCon((WindowPtr)theEvent.message))).windowIndex;
  154.     else if (gFrontWindowIsOurs)    /* if front window is ours, get its window index */
  155.         index=gFrontWindowIndex;
  156.     else index=-1;
  157.     
  158.     switch (theEvent.what)
  159.     {
  160.         case nullEvent:    /* ain't nuthin' happenin' */
  161.             if ((gSoundAvailable) && (gSoundIsFinishedPlaying))
  162.                 CloseTheSoundChannel();
  163.             if (gFrontWindowIsOurs)        /* give control to window dispatch to handle null */
  164.                 CallIndDispatchProc(index, kNull, 0L);
  165.             break;
  166.         case mouseDown:    /* mouse button pressed */
  167.             HandleMouseDown(theEvent);    /* see below for mousedown handling */
  168.             break;
  169.         case keyDown:    /* key pressed */
  170.         case autoKey:    /* key help down */
  171.             if (theEvent.modifiers & cmdKey)    /* handle as command-key equivalent */
  172.             {
  173.                 AdjustMenus();    /* just to be safe */
  174.                 /* get the menu ID + item and handle it as a menu choice */
  175.                 HandleMenu(MenuKey((char)(theEvent.message & charCodeMask)));
  176.             }
  177.             else if (gFrontWindowIsOurs)    /* --> window's dispatch for keydown */
  178.                 CallIndDispatchProc(index, kKeydown, theEvent.message);
  179.             break;
  180.         case diskEvt:    /* disk insert */
  181.             if (HiWord(theEvent.message)!=noErr)    /* bad disk inserted */
  182.             {
  183.                 DILoad();    /* load disk initialization package */
  184.                 SetPt(&thisPoint, 120, 120);
  185.                 DIBadMount(thisPoint, theEvent.message);    /* give format? dialog */
  186.                 DIUnload();    /* unload 'cuz we certainly don't need it */
  187.             }
  188.             break;
  189.         case updateEvt:    /* window update */
  190.             theWindow=(WindowPtr)theEvent.message;    /* which window? */
  191.             
  192.             BeginUpdate(theWindow);        /* means: "OK, we're dealing with this now" */
  193.             
  194.             if (thisWindowIsOurs)        /* one of ours?  see graphics.c */
  195.                 UpdateTheWindow(GetIndWindowDataHandle(index));
  196.             /* if not, could be our progress bar */
  197.             else if ((theWindow!=0L) && (theWindow==gProgressDlog))
  198.                 UpdateDialog(theWindow, theWindow->visRgn);
  199.             
  200.             EndUpdate(theWindow);        /* means: "OK, we're done updating now" */
  201.             break;
  202.         case activateEvt:    /* window activate or deactivate */
  203.             if (thisWindowIsOurs)        /* one of ours?  send message to window dispatch */
  204.                 CallIndDispatchProc(index, ((theEvent.modifiers&activeFlag)!=0) ?
  205.                     kActivate : kDeactivate, 0L);
  206.             break;
  207.         case osEvt:            /* suspend or resume program execution (switch in/out) */
  208.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  209.             {
  210.                 /* keep track of whether we're in the background or foreground */
  211.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  212.                 
  213.                 if (gFrontWindowIsOurs)        /* send activate/deactivate to front window */
  214.                     CallIndDispatchProc(index, gIsInBackground ? kDeactivate : kActivate, 0L);
  215.                 
  216.                 for (i=0; i<NUM_WINDOWS; i++)    /* send suspend/resume to all our */
  217.                     if (GetIndWindowGrafPtr(i)!=0L)        /* windows that currently exist */
  218.                         CallIndDispatchProc(i, gIsInBackground ? kSuspend : kResume, 0L);
  219.                 
  220.                 /* if we just came into the foreground and we have a pending error,
  221.                    now's the time to display it */
  222.                 if ((!gIsInBackground) && (gPendingResultCode!=allsWell))
  223.                 {
  224.                     if (gHasNotificationManager)
  225.                         NMRemove(&gMyNotification);        /* remove notification request */
  226.                     HandleError(gPendingResultCode, FALSE);    /* display alert, see error.c */
  227.                     gPendingResultCode=allsWell;        /* ...now it is */
  228.                 }
  229.             }
  230.             break;
  231.         case kHighLevelEvent:    /* apple event */
  232.             if (gHasAppleEvents)
  233.                 AEProcessAppleEvent(&theEvent);        /* see apple events.c */
  234.             break;
  235.     }
  236. }
  237.  
  238. void HandleMouseDown(EventRecord theEvent)
  239. {
  240.     WindowPtr            theWindow;
  241.     short                windowCode;
  242.     long                windSize;
  243.     GrafPtr                oldPort;
  244.     short                i;
  245.     Rect                sizeRect;
  246.     Boolean                gotone;
  247.     short                index;
  248.     unsigned long        dummy;
  249.     Point                theLocalPoint;
  250.     Boolean                thisWindowIsOurs;
  251.     ExtendedWindowDataHandle    theData;
  252.     
  253.     windowCode=FindWindow(theEvent.where, &theWindow);    /* which window? */
  254.  
  255.     thisWindowIsOurs=FALSE;
  256.     /* find out if the target window was one of ours */
  257.     if (theWindow!=0L)
  258.         for (index=0; ((index<NUM_WINDOWS) && (!thisWindowIsOurs)); index++)
  259.             thisWindowIsOurs=(theWindow==GetIndWindowGrafPtr(index));
  260.  
  261.     if (thisWindowIsOurs)    /* if target window is one of ours, get window data struct */
  262.     {
  263.         theData=(ExtendedWindowDataHandle)GetWRefCon(theWindow);
  264.         index=(**theData).windowIndex;
  265.     }
  266.     else index=-1;
  267.     
  268.     switch (windowCode)
  269.     {
  270.         case inMenuBar:        /* in menu bar; let system take over */
  271.             AdjustMenus();
  272.             HandleMenu(MenuSelect(theEvent.where));
  273.             break;
  274.         case inContent:        /* in window content */
  275.             if (FrontWindow() != theWindow)        /* maybe switch to different window */
  276.             {
  277.                 if (FrontWindow()==gProgressDlog)    /* but not if progress bar is up */
  278.                     SysBeep(7);
  279.                 else SelectWindow(theWindow);
  280.             }
  281.             else if (gFrontWindowIsOurs)    /* inform window dispatch of mousedown */
  282.             {
  283.                 theLocalPoint=theEvent.where;
  284.                 GlobalToLocal(&theLocalPoint);
  285.                 dummy=theLocalPoint.h;        /* all this fiddling is so we can pass */
  286.                 dummy=dummy<<16;            /* the point (two integers) to the */
  287.                 dummy+=theLocalPoint.v;        /* dispatch procedure in an unsigned long */
  288.                 CallIndDispatchProc(index, kMousedown, dummy);    /* go for it */
  289.             }
  290.             break;
  291.         case inSysWindow:    /* in system window (desk accessory) */
  292.             SystemClick(&theEvent, theWindow);    /* let the system deal with it */
  293.             break;
  294.         case inDrag:        /* in drag _region_, that is */
  295.             /* the accepted way to draw a window */
  296.             DragWindow(theWindow, theEvent.where, &((**GetGrayRgn()).rgnBBox));
  297.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  298.                 (**theData).windowBounds=
  299.                     (*(((WindowPeek)GetIndWindowGrafPtr(index))->contRgn))->rgnBBox;
  300.             break;
  301.         case inGoAway:        /* close box */
  302.             /* the accepted way to track a close box attempt */
  303.             if (TrackGoAway(theWindow, theEvent.where))
  304.                 DoTheCloseThing((WindowPeek)theWindow);        /* see menus.c */
  305.             break;
  306.         case inGrow:        /* grow box */
  307.             /* the accepted way to grow a window */
  308.             sizeRect = screenBits.bounds;
  309.             OffsetRect(&sizeRect, sizeRect.left, sizeRect.top);
  310.             
  311.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  312.             if (windSize != 0)
  313.             {
  314.                 GetPort(&oldPort);
  315.                 SetPort(theWindow);
  316.                 EraseRect(&theWindow->portRect);
  317.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  318.                 InvalRect(&theWindow->portRect);
  319.                 SetPort(oldPort);
  320.             }
  321.             
  322.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  323.                 (**theData).windowBounds=
  324.                     (*(((WindowPeek)GetIndWindowGrafPtr(index))->contRgn))->rgnBBox;
  325.             break;
  326.         case inZoomIn:        /* zoom box */
  327.         case inZoomOut:
  328.             /* the accepted way to track a zoom attempt */
  329.             if (TrackBox(theWindow, theEvent.where, windowCode))
  330.             {
  331.                 GetPort(&oldPort);
  332.                 SetPort(theWindow);
  333.                 ZoomWindow(theWindow, windowCode, FALSE);
  334.                 InvalRect(&theWindow->portRect);
  335.                 SetPort(oldPort);
  336.             }
  337.             
  338.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  339.                 (**theData).windowBounds=
  340.                     (*(((WindowPeek)GetIndWindowGrafPtr(index))->contRgn))->rgnBBox;
  341.             break;
  342.     }
  343. }
  344.  
  345. void ShutDownEnvironment(void)
  346. {
  347.     SaveThePrefs();
  348.     ShutDownTheProgram();        /* program-specific cleanup */
  349.     ShutDownTheGraphics();        /* shell-specific clean-up */
  350.     CloseTheSoundChannel();        /* dispose of sound channel, if exists */
  351. }
  352.